// 包名和模块引入
package com.example.coffee.slice;
import com.example.coffee.ResourceTable;
import com.example.coffee.items.CaffeeItem;
import com.example.coffee.items.IndexBarItem;
import com.example.coffee.mock.DataMockManager;
import com.example.coffee.provider.ContentListDataProvider;
import com.example.coffee.provider.IndexBarDataProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;

import java.util.List;
// 类实现
public class MainAbilitySlice extends AbilitySlice {
    // 列表组件
    private ListContainer indexBarList = null;
    private ListContainer contentList = null;

    private Button searchButton = null;
    private Component storeComp = null;

    private Text placeLabel = null;
    private Text distanceLabel = null;

    // 分类列表组件的数据Provider实例和数据源
    private List<IndexBarItem> indexBarData = null;
    private IndexBarDataProvider indexBarDataProvider = null;
    // 餐品列表组件的数据Provider实例和数据源
    private List<CaffeeItem> caffeeItems = null;
    private ContentListDataProvider contentListDataProvider = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 从布局文件获取子组件
        findComponents();
        // 进行初始化
        setup();
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode != 10) {
            return;
        }
        String place = resultIntent.getStringParam("address");
        String distance = resultIntent.getStringParam("distance");
        placeLabel.setText(place);
        distanceLabel.setText(distance);
    }

    // 获取子组件
    private void findComponents() {
        indexBarList = (ListContainer) findComponentById(ResourceTable.Id_list_index_bar);
        contentList = (ListContainer) findComponentById(ResourceTable.Id_list_content);

        searchButton = (Button) findComponentById(ResourceTable.Id_search_button);
        searchButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new SearchSlice(), new Intent());
            }
        });

        storeComp = findComponentById(ResourceTable.Id_store);
        storeComp.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                presentForResult(new StoreSlice(), new Intent(), 10);
            }
        });

        placeLabel = (Text) findComponentById(ResourceTable.Id_place_label);
        distanceLabel = (Text) findComponentById(ResourceTable.Id_distance_label);
    }
    // 初始化方法
    private void setup() {
        // 生成模拟数据
        indexBarData = DataMockManager.mockIndexBarData();
        caffeeItems = DataMockManager.mockCaffeeList(indexBarData.get(0).getName());
        // 将分类列表数据源中的第一个分类设置为默认选中的分类
        indexBarData.get(0).setSelected(true);
        // 创建Provider实例
        indexBarDataProvider = new IndexBarDataProvider(indexBarData,this);
        contentListDataProvider = new ContentListDataProvider(caffeeItems, this);
        // 设置列表的Provider以及提那家监听
        indexBarList.setItemProvider(indexBarDataProvider);
        indexBarList.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                indexBarDataProvider.notifyDataChanged();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
            }
        });
        contentList.setItemProvider(contentListDataProvider);
        contentList.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                contentListDataProvider.notifyDataChanged();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
            }
        });
        // 为类别列表的列表项点击设置监听
        indexBarList.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long l) {
                // 遍历数据源，将所有类别设置为非选中状态（为了清除之前的选中态）
                for (IndexBarItem indexBarDatum : indexBarData) {
                    indexBarDatum.setSelected(false);
                }
                // 将当前用户点击的类别设置为选中状态
                indexBarData.get(position).setSelected(true);
                // 刷新类别列表
                indexBarDataProvider.notifyDataChanged();
                // 根据类别重新生成餐品列表
                caffeeItems = DataMockManager.mockCaffeeList(indexBarData.get(position).getName());
                // 重新设置餐品列表Provider的数据源
                contentListDataProvider.setDataList(caffeeItems);
                // 刷新餐品列表
                contentListDataProvider.notifyDataChanged();
            }
        });
        contentList.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                CaffeeItem item = caffeeItems.get(i);
                Intent intent = new Intent();
                intent.setParam("image", item.getImageId());
                intent.setParam("name", item.getName());
                intent.setParam("description", item.getDescription());
                intent.setParam("price", item.getPrice());
                intent.setParam("originalPrice", item.getOriginalPrice());
                present(new DetailSlice(), intent);
            }
        });
    }
}
